package controls;

import java.util.Observable;
import java.util.Observer;

import models.ServicoTNode;
import models.UsuarioLink;

import views.PanFunCadastrarServico;
import views.WinFuncionario;

public class ControllerFunCadastrarServico implements Observer {
	private PanFunCadastrarServico pan_fun_cadastrar_servico = new PanFunCadastrarServico();
	private ServicoTNode newNode;
	private WinFuncionario winFuncionario;

	public void setPanel(WinFuncionario windowFuncionario){
		winFuncionario = windowFuncionario;
		
		pan_fun_cadastrar_servico.criaPanel();
		pan_fun_cadastrar_servico.addObserver(this);
		
		winFuncionario.setContent(pan_fun_cadastrar_servico.getPanel());
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		String temp = new String((String) arg1);
		if(!temp.contains("&")){
			String data_form = temp;
			UsuarioLink cliente = (UsuarioLink) ControllerLogin.getPersistence().find("UsuarioLinkListString",data_form);
			pan_fun_cadastrar_servico.setCliente(ControllerLogin.getPersistence().list("VeiculoTree", cliente.getUsuarioId()));
		}
		else{
			String[] data_form = new String[4];
			data_form = temp.split("&");
			int veicID = Integer.parseInt(data_form[0]);
			int modServID = Integer.parseInt(data_form[2]);
			newNode = new ServicoTNode(veicID, data_form[1], modServID, data_form[3]);
			ControllerLogin.getPersistence().insert("ServicoTree",newNode);
			ControllerFunVerServico verServ = new ControllerFunVerServico();
			verServ.setPanel(newNode, winFuncionario);
		}
	}
}
